package com.jiankang.askhealth.util;

import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class ApiErrorMsgUtil {

    private static Map<String,String> errorMsgMap ;

    static {
        errorMsgMap = new HashMap<String,String>();
        Properties paramConfig = new Properties();
        InputStream ins = null;
        try {
            ins = ApiErrorMsgUtil.class.getClassLoader().getResourceAsStream("ApiErrorMessage.properties");
            paramConfig.load(ins);
            Set<String> keySet = paramConfig.stringPropertyNames();
            for(String key : keySet) {
                String param = paramConfig.getProperty(key, "未知错误");
                errorMsgMap.put(key, param);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(ins != null) {
                try {
                    ins.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ins = null;
            }
        }
    }

    public static String getErrorMsg(int errorCode) {
        return getErrorMsg(errorCode, null);
    }

    public static String getErrorMsg(int errorCode, Object[] args) {
        String msg = errorMsgMap.get("errorCode_" + errorCode);
        if(args != null && args.length > 0) {
            msg = MessageFormat.format(msg, args);
        }
        return msg;
    }

}

